import { map } from 'rxjs/operators';
import { NzTreeNodeOptions } from 'ng-zorro-antd/tree';
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
import { PhasedTask } from 'src/app/model/task';
import { Observable, EMPTY } from 'rxjs';
import { PhasedTaskApiService } from 'src/app/api/phased-task-api.service';

@Injectable()
export class PhaseTaskTreeResolverService implements Resolve<NzTreeNodeOptions>{

  constructor(
    private taskApi: PhasedTaskApiService,
  ) { }
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
    : NzTreeNodeOptions | Observable<NzTreeNodeOptions> | Promise<NzTreeNodeOptions> {
    const id = route.paramMap.get('id');
    if (id) {
      return this.taskApi.listTreeView(parseInt(id, 10)).pipe(
        map(treeItem => treeItem),
      );
  }
    return EMPTY;
  }
}
